#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "los_swtmr.h"

// 定义UNUSED宏
#ifndef UNUSED
#define UNUSED(x) (void)(x)
#endif

UINT32 g_timerCount1 = 0;
UINT32 g_timerCount2 = 0;

VOID Timer1_CallBack(UINT32 arg)
{
    UINT64 lastTick;

    UNUSED(arg);

    g_timerCount1++;
    lastTick = LOS_TickCountGet();
    dprintf("g_timerCount1=%u\n", g_timerCount1);
    dprintf("tick_last1=%llu\n", (unsigned long long)lastTick);
}

VOID Timer2_CallBack(UINT32 arg)
{
    UINT64 lastTick;

    UNUSED(arg);

    lastTick = LOS_TickCountGet();
    g_timerCount2++;
    dprintf("g_timerCount2=%u\n", g_timerCount2);
    dprintf("tick_last2=%llu\n", (unsigned long long)lastTick);
}

VOID Timer_example(VOID)
{
    UINT16 id1;     // Timer1 id
    UINT16 id2;     // Timer2 id
    UINT32 tick;

    // 创建并启动Timer1
    if (LOS_SwtmrCreate(1000, LOS_SWTMR_MODE_ONCE, Timer1_CallBack, &id1, 1) == LOS_OK) {
        dprintf("Create Timer1 success\n");
    }
    
    if (LOS_SwtmrStart(id1) == LOS_OK) {
        dprintf("start Timer1 success\n");
    }

    LOS_TaskDelay(200);
    if (LOS_SwtmrTimeGet(id1, &tick) == LOS_OK) {
        dprintf("tick =%u\n", tick);
    }

    LOS_SwtmrStop(id1);
    dprintf("Stop Timer1 success\n");

    LOS_SwtmrStart(id1);
    LOS_TaskDelay(1000);
    LOS_SwtmrDelete(id1);
    dprintf("delete Timer1 success\n");

    // 创建并启动Timer2
    if (LOS_SwtmrCreate(100, LOS_SWTMR_MODE_PERIOD, Timer2_CallBack, &id2, 1) == LOS_OK) {
        dprintf("Start Timer2\n");
    }

    LOS_SwtmrStart(id2);
    LOS_TaskDelay(1000);
    LOS_SwtmrStop(id2);
    LOS_SwtmrDelete(id2);
    dprintf("delete Timer2 success\n");
}

APP_FEATURE_INIT(Timer_example);